package beans;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Random;

public class GameInterface
{
	//属性声明
		GameFrame	mainFrame;//主框架
		public GameInterface(GameFrame mainFrame)
		{
			this.mainFrame=mainFrame;
			prepare();//游戏准备：初始化
		}	
		ArrayList<Sun> sunList;//阳光数组列表
		ArrayList<Zombie> zombieList;//僵尸数组列表
		ArrayList<Bullet> bulletList;//子弹数组列表
		ArrayList<Card> cardList;//卡片数组列表
		BackGround back;//背景
		Plant[][] plantArray;//植物数组
		Plant pea;	//豌豆射手
		Plant sunFlower;//向日葵
		Plant potato;//土豆
		Plant bomb;//炸弹
		Plant ice;//寒冰
		Plant pepper;//辣椒
		int sunCount;//阳光总数
		MusicPlayer music;//音乐
		int zombieDieAmount;//僵尸死亡总数
		String uid="小波666";
		
	void prepare()
	{
		sunList = new ArrayList<Sun>();
		zombieList  =  new ArrayList<Zombie>();
		bulletList = new ArrayList<Bullet>();
		cardList = new ArrayList<Card>();
		cardList.add(new Card(0,"射手"));
		cardList.add(new Card(1,"向日葵"));
		cardList.add(new Card(2,"坚果"));
		cardList.add(new Card(3,"炸弹"));
		cardList.add(new Card(4,"冰冻豆瓣"));
		cardList.add(new Card(5,"辣椒"));
		pea = new Plant();//单个射手：指定射手
		ice = new Plant();//寒冰射手
		sunFlower = new Plant();
		potato = new Plant();
		bomb = new Plant();
		pepper=new Plant();
		back = new BackGround();

		plantArray = new Plant[5][9];//5行9列，45个植物位置
		sunCount  = 1000;//太阳值 初始值
		music = new MusicPlayer("植物大战僵尸/声音/僵尸大战植物背景音乐.wav");
//		for (int h = 0; h < 5; h++)
//		{
//			for (int l = 0; l < 9; l++)
//			{
//				zws[h][l]= new 植物(0,l*80,h*100);
//			}
//		}
	}

	void mouseClickActivity(int  mousex,int mousey )//鼠标点击后可能的情况
	{   //卡片数量
		for(  int cardAmount= 0 ; cardAmount<= cardList.size()-1  ; cardAmount++ )//循环所有卡片
		{
			if(cardList.get(cardAmount).name=="射手"&&cardList.get(cardAmount).state==1)//如果是射手并且卡片是暗的
			{
				int arrayRow = (mousey-81)/100;
				int arrayColumn = (mousex-34)/80;

				Plant newPea = new Plant(0,arrayColumn*80,arrayRow*100);
				plantArray[arrayRow][arrayColumn] = newPea;
				sunCount-=100;
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;//让卡片变亮
				pea.removeShadeState();//删除豌豆虚影
			}
			
			if(cardList.get(cardAmount).name=="射手"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=100)
			{				//如果是射手，且点击了射手卡片图，且阳光值是够的
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				
				if(pea==null)
				{
					pea = new Plant(0,mousex,mousey,6);
				}
				else
				{
					pea.paintShadeState();
				}
			}

			if(cardList.get(cardAmount).name=="向日葵"&&cardList.get(cardAmount).state==1)//如果是向日葵并且卡片是暗的
			{
				int arrayRow = (mousey-81)/100;//植物数组的行 ，控制y
				int arrayColumn = (mousex-34)/80;//植物数组的列 ，控制x

				Plant newSunFlower = new Plant(1,arrayColumn*80,arrayRow*100);
				plantArray[arrayRow][arrayColumn] = newSunFlower;//新的向日葵 在row行column列 放了向日葵 
				sunCount-=100;//如果鼠标已经点击向日葵卡片后，阳光值要减少100
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				sunFlower.removeShadeState();//移除向日葵虚影
			}
			
			if(cardList.get(cardAmount).name=="向日葵"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=50)//如果是向日葵，且点击了向日葵卡片图，且阳光值是够的
			{				
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				if(sunFlower==null)
				{
					sunFlower = new Plant(1,mousex,mousey,6);
				}
				else
				{
					sunFlower.paintShadeState();
				}
				
			}

			if(cardList.get(cardAmount).name=="坚果"&&cardList.get(cardAmount).state==1)//如果是坚果并且卡片是暗的
			{
				int arrayRow = (mousey-81)/100;
				int arrayColumn = (mousex-34)/80;
				Plant newPotato = new Plant(2,arrayColumn*80,arrayRow*100);
				plantArray[arrayRow][arrayColumn] = newPotato;//新土豆
				sunCount-=50;
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				potato.removeShadeState();//移除土豆虚影
			}
			if(cardList.get(cardAmount).name=="坚果"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=50)//如果是坚果，且点击了射手卡片图，且阳光值是够的
			{				
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				if(potato==null)
				{
					potato = new Plant(2,mousex,mousey,6);
				}
				else
				{
					potato.paintShadeState();
				}
			}
			
			if(cardList.get(cardAmount).name=="炸弹"&&cardList.get(cardAmount).state==1)//如果是炸弹并且卡片是暗的
			{
				int arrayRow = (mousey-81)/100;
				int arrayColumn = (mousex-34)/80;
				Plant newBomb = new Plant(3,arrayColumn*80,arrayRow*100);
				plantArray[arrayRow][arrayColumn] = newBomb;//新炸弹
				sunCount-=150;
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				bomb.removeShadeState();
			}
			if(cardList.get(cardAmount).name=="炸弹"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=150)//如果是炸弹，且点击了射手卡片图，且阳光值是够的
			{				
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				if(bomb==null)
				{
					bomb = new Plant(3,mousex,mousey,6);
				}
				else
				{
					bomb.paintShadeState();
				}
			}
			
			if(cardList.get(cardAmount).name=="冰冻豆瓣"&&cardList.get(cardAmount).state==1)//如果是寒冰并且卡片是暗的
			{
				int row = (mousey-81)/100;
				int column = (mousey-34)/80;

				Plant newIce = new Plant(4,column*80,row*100);
				plantArray[row][column] = newIce;//新来的寒冰射手
				sunCount-=100;
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;//让卡片变亮
				ice.removeShadeState();//删除寒冰虚影

			}
			if(cardList.get(cardAmount).name=="冰冻豆瓣"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=100)//如果是寒冰，且点击了寒冰卡片图，且阳光值是够的
			{				
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				
				if(ice==null)
				{
					ice = new Plant(0,mousex,mousey,6);
				}
				else
				{
					ice.paintShadeState();
				}
			}
			
			if(cardList.get(cardAmount).name=="辣椒"&&cardList.get(cardAmount).state==1)//如果是辣椒并且卡片是暗的
			{
				int row = (mousey-81)/100;
				int column = (mousex-34)/80;
				Plant newPepper= new Plant(5,column*80,row*100);
				plantArray[row][column] = newPepper;//新辣椒
				sunCount-=150;
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				pepper.removeShadeState();
			}
			if(cardList.get(cardAmount).name=="辣椒"&&cardList.get(cardAmount).isClick(mousex, mousey)&&sunCount>=150)//如果是炸弹，且点击了射手卡片图，且阳光值是够的
			{				
				cardList.get(cardAmount).state++;
				cardList.get(cardAmount).state%=2;
				if(pepper==null)
				{
					pepper = new Plant(5,mousex,mousey,6);
				}
				else
				{
					pepper.paintShadeState();
				}
			}
		}
		
	
		for(int sunAmount= 0 ; sunAmount<= sunList.size()-1; sunAmount++)//循环所有阳光
		{
			if(sunList.get(sunAmount).isClick( mousex , mousey))//如果点击了阳光   ///////////点击后移动阳光功能
			{
				sunList.get(sunAmount).state=3;
				sunCount+=50;
			}
		}

		//如果点击左下角退出键，
		if(new Rectangle(720,0,80,60).contains(mousex, mousey))
		{	
			User user=new User();
			user.setZombieDieMaxAmount(zombieDieAmount);
			System.out.println("最终成绩："+zombieDieAmount);
			mainFrame.gamePicture.music.stopPlayer();//停止音乐
			mainFrame.gamePicture=null;//开始界面改为0
			mainFrame.startPicture = new StartInterface(mainFrame);//把主框架放入进入开始界面，实例化
			mainFrame.startPicture.picture=2;//让他直接到第二个界面
			//mainFrame.startPicture.StartInterface();///初始化游戏界面
		}
	}

	void moveMouse(int  mousex,int mousey )//移动鼠标 ：点击选项卡后的虚影坐标：用鼠标的位置来得到虚影的坐标
	{
		if(pea!=null&&pea.state==6)
		{
			pea.x=mousex;
			pea.y=mousey;
		}
		if(sunFlower!=null&&sunFlower.state==6)
		{
			sunFlower.x=mousex;
			sunFlower.y=mousey;
		}
		if(potato!=null&&potato.state==6)
		{
			potato.x=mousex;
			potato.y=mousey;
		}
		if(bomb!=null&&bomb.state==6)
		{
			bomb.x=mousex;
			bomb.y=mousey;
		}
		if(ice!=null&&ice.state==6)
		{
			ice.x=mousex;
			ice.y=mousey;
		}
		if(pepper!=null&&pepper.state==6)
		{
			pepper.x=mousex;
			pepper.y=mousey;
		}
	}
	void paintPicture(Graphics g)//画界面
	{
		//背景显示
		back.paint(g);
		//卡片显示
		for(  int cardAmount= 0 ; cardAmount<= cardList.size()-1  ; cardAmount++ )//绘制所有卡片
		{
			cardList.get(cardAmount).paint(g);
		}
		//植物显示
		for (int row = 0; row < 5; row++)
		{
			for (int column = 0; column < 9; column++)
			{
				if(plantArray[row][column]!=null)
				{
					plantArray[row][column].paint(g);
				}
			}
		}
		//绘制植物虚影
		if(pea!=null)//绘制射手虚影
		{
			pea.paintPeaShade(g);
		}
		if(sunFlower!=null)//绘制向日葵虚影
		{
			sunFlower.paintSunFlowerShade(g);
		}
		if(potato!=null)//绘制坚果虚影
		{
			potato.paintPotatoShade(g);
		}
		if(bomb!=null)//绘制炸弹虚影
		{
			bomb.paintBombShade(g);
		}
		if(ice!=null)//绘制寒冰虚影
		{
			ice.paintIceShade(g);
		}
		if(pepper!=null)//绘制辣椒虚影
		{
			pepper.paintPepperShade(g);
		}
		//僵尸显示
		for(int zombieAmount = 0; zombieAmount <= zombieList.size()-1; zombieAmount++)//绘制所有僵尸
		{
			zombieList.get(zombieAmount).paint(g);
		}
		//子弹显示
		for(int bulletAmount = 0; bulletAmount <= bulletList.size()-1; bulletAmount++)//绘制所有子弹
		{
			bulletList.get(bulletAmount).paint(g);
		}
		//阳光显示
		for(int sunAmount =0; sunAmount <=sunList.size()-1; sunAmount++ )//绘制所有太阳
		{
			sunList.get(sunAmount).paintSun(g);
		
		g.setFont(new Font("楷体", 0, 15));
		g.drawString(""+sunCount, 40, 80);
		}
	}

	void timingProcess()//定时处理
	{
		
		music.startPlayer();

		//0.随机生产僵尸================
		if(new Random().nextInt(600)<30)
		{//1.造僵尸
			int  x = 800;
			int  y = 100*new Random().nextInt(5);
			//2.装入容器
			Zombie newZombie = new Zombie(x,y);
			zombieList.add(newZombie);//不断向僵尸容器中加入新僵尸
		}
		//1.子弹活动===================
		for(  int bulletAmount= 0 ; bulletAmount<= bulletList.size()-1; bulletAmount++)//遍历所有子弹
		{
			bulletList.get(bulletAmount).activity(zombieList);//子弹与僵尸的活动
		}
		//2.植物活动====================
		for (int row = 0; row < 5; row++)
		{
			for (int column = 0; column < 9; column++)
			{
				if(plantArray[row][column]!=null)
				{
	
					plantArray[row][column].activity(zombieList, bulletList,sunList);//植物与僵尸或子弹或太阳的活动
					if(plantArray[row][column].state==4)

						plantArray[row][column]=null;
				}
			}
		}
		//3.僵尸换张与行走====================

		for(int zombieAmount= 0 ;zombieAmount<= zombieList.size()-1; zombieAmount++)//遍历所有僵尸
		{
			zombieList.get(zombieAmount).activity(plantArray);
			if(zombieList.get(zombieAmount).state==4)
			{
				zombieDieAmount++;
				System.out.println("击杀僵尸数量: "+zombieDieAmount);
				zombieList.remove(zombieAmount);
			}
		}
		
		//4.阳光活动=======================
		if(new Random().nextInt(600)<20)//20/600的概率出现阳光
		{
			Sun newSun = new Sun();//实例化一个太阳
			sunList.add(newSun);//增加一个太阳
		}
		for( int sunAmount =0 ; sunAmount <=sunList.size()-1 ; sunAmount++ )
		{
			if(sunList.get(sunAmount).state==3) //不能同时进行两个线程
			{
				sunList.get(sunAmount).sunClick();
			}
			else {
				sunList.get(sunAmount).sunDown();
				sunList.get(sunAmount).sunChangePage();	
				////////阳光移动到左上角？？？？state=3已经被点击
				if(sunList.get(sunAmount).state==2)
				{
					sunList.remove(sunAmount);
				}
			}
		}
		
		
	}

}

